package com.aiss.ws.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;


import com.aiss.ws.MyApplication;
import com.aiss.ws.activity.BaseActivity;
import com.aiss.ws.activity.RegistActivity_;
import com.aiss.ws.httptools.GetJSONObjectPostUtil;
import com.aiss.ws.httptools.GetJsonListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class Futil {

	@SuppressWarnings("unused")
	protected static HttpHandler<String> httpPost;
	
	public static void showMessage(Context context, String string){
		Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
	}

	private static String PREF_SHARE = "information";

	/**
	 * 清空
	 * @param context
	 */
	public static void clearValues(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SHARE,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}




	/**
	 * 向SP文件存储数据
	 *
	 * @param context
	 * @param key
	 *            键名
	 * @param value
	 *            键值
	 */
	public static void saveValue(Context context, String key, Object value,
								 int type) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SHARE,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		switch (type) {
			case 1:
				editor.putInt(key, (Integer) value);
				break;

			case 2:
				editor.putString(key, (String) value);
				break;

			case 3:
				editor.putBoolean(key, (Boolean) value);
				break;
		}
		editor.commit();
	}

	/**
	 * 从SP文件中读取指定Key的值
	 *
	 * type=1/数值 defValue=-1 | type=2/字符串 defValue=null | type=3/布尔
	 * defValue=false
	 *
	 * @param context
	 * @param key
	 *            键名
	 * @param type
	 *            数据存储类型
	 * @return 键值
	 */
	public static Object getValue(Context context, String key, int type) {
		SharedPreferences sp = context.getSharedPreferences(PREF_SHARE,
				Context.MODE_PRIVATE);
		Object object = null;
		switch (type) {
			case 1:
				object = sp.getInt(key, 0);
				break;

			case 2:
				if(sp.getString(key, "")==null){
					break;
				}
				object = sp.getString(key, "");
				break;

			case 3:
				object = sp.getBoolean(key, false);
				break;
		}
		return object;
	}


	public static void AddHashMap(HashMap<String, String> map){
		map.put("did", "2");
		map.put("dkey", "bc_android");
		map.put("dsecret", "7f58358410b58c3295b4db1c812b1f7d");

		//登陆情况下
		if(!getValue(MyApplication.getContext(),Command.MEMBER_ID,2).toString().equals("")&&!getValue(MyApplication.getContext(),Command.SESSION_KEY,2).toString().equals("")){
			map.put(Command.SESSION_KEY,getValue(MyApplication.getContext(),Command.SESSION_KEY,2).toString());
			map.put(Command.MEMBER_ID,getValue(MyApplication.getContext(),Command.MEMBER_ID,2).toString());
		}




	}

	public static void changejsonb(JSONObject jsonObject){



	}
//	private static ProgressDialog dialog;

	public static void xutils(String url, HashMap<String, String> map, final Handler handler, final int code){
		xutils(url,map,handler,code, BaseActivity.getContext());
	}


		public static void xutils(String url, HashMap<String, String> map, final Handler handler, final int code,Context context){

//		if(dialog==null){
//			dialog = new ProgressDialog(context);
//			dialog.setMessage("正在加载中");
//		}
//		dialog.show();

		AddHashMap(map);
		httpPost = new GetJSONObjectPostUtil(url, map, new GetJsonListener() {

			@Override
			public void onSuccessed(JSONObject result) {
//				if(dialog!=null&&dialog.isShowing()){
//					dialog.cancel();
//				}

				try {
					String state = result.getString("state");
					if(state.equals("1")){
						Message msg = new Message();
						msg.obj = result;
						msg.what  = code;
						handler.sendMessage(msg);

					}else if(state.equals("4")){//账号异常
						Futil.showMessage(MyApplication.getContext(), result.getString("return_data"));

						Handler handler = new Handler();
						handler.postDelayed(new Runnable() {
							@Override
							public void run() {
								Intent intent = new Intent();
								intent.setClass(MyApplication.getContext(), RegistActivity_.class);
								intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								MyApplication.getContext().startActivity(intent);
							}
						}, 500);

					}else if(state.equals("5")){//补全信息
						Futil.showMessage(MyApplication.getContext(), result.getString("return_data"));
						Intent intent = new Intent();
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//						MyApplication.getContext().startActivity(intent);
					}else{
						handler.sendEmptyMessage(1000);
						Futil.showMessage(MyApplication.getContext(), result.getString("return_data"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}


			}

			@Override
			public void onFailed(HttpException error, String msg) {
//				if(dialog!=null&&dialog.isShowing()){
//					dialog.cancel();
//				}
				Futil.showMessage(MyApplication.getContext(), msg);
			}
		}).getHttpHandler();
	}

	public static void xutils_answer(String url, HashMap<String, String> map, final Handler handler, final int code){
		AddHashMap(map);
		httpPost = new GetJSONObjectPostUtil(url, map, new GetJsonListener() {

			@Override
			public void onSuccessed(JSONObject result) {

				try {
					String state = result.getString("state");
					if(state.equals("1")){
						Message msg = new Message();
						msg.obj = result;
						msg.what  = code;
						handler.sendMessage(msg);

					}else if(state.equals("4")){//账号异常
						Futil.showMessage(MyApplication.getContext(), result.getString("return_data"));

						Handler handler = new Handler();
						handler.postDelayed(new Runnable() {
							@Override
							public void run() {
								Intent intent = new Intent();
								intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								MyApplication.getContext().startActivity(intent);
							}
						}, 500);

					}else if(state.equals("5")){//补全信息
						Futil.showMessage(MyApplication.getContext(), result.getString("return_data"));
						Intent intent = new Intent();
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						MyApplication.getContext().startActivity(intent);
					}else{
						Futil.showMessage(MyApplication.getContext(), result.getString("return_data"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}


			}

			@Override
			public void onFailed(HttpException error, String msg) {
				Futil.showMessage(MyApplication.getContext(), msg);
			}
		}).getHttpHandler();
	}


	

}
